package retryable

import (
	"context"
	"errors"
	"jk-time/webook/internal/service/sms"
	"sync/atomic"
)

type RetryableService struct {
	svc sms.Service
	//重试
	retryMax int
}

func NewRetryableService(svc sms.Service, retryMax int) sms.Service {
	return &RetryableService{svc: svc, retryMax: retryMax}
}
func (r RetryableService) Send(ctx context.Context, biz string, args []string, numbers ...string) error {
	err := r.svc.Send(ctx, biz, args, numbers...)
	var cnt int32
	atomic.StoreInt32(&cnt, 1)

	for err != nil && atomic.LoadInt32(&cnt) < int32(r.retryMax) {
		err := r.svc.Send(ctx, biz, args, numbers...)
		if err == nil {
			return nil
		}
		atomic.AddInt32(&cnt, 1)
	}
	return errors.New("重试都失败了")
}
